
#if	!defined( __20100830_DEF_SPINLOCK_H__ )
#define	__20100830_DEF_SPINLOCK_H__

namespace PCE_Lib {
namespace CEnv_Def {
namespace PCE_Lib_CurrentCEnvironment {
namespace Tools {

#if	defined( PCE_CFG_OS_Windows )

class SpinLock_t
{
public:
	SpinLock_t( void ) {}
	~SpinLock_t( void ) {}
};

#elif defined( PCE_CFG_OS_Linux )

// linux version
class SpinLock_t
{
public:
	bool Try( void )
	{
		bool	ret = true;

		__asm__ __volatile__ (
			"lock;" "xchgb	%0, %1"
			:"+r"(ret),
			 "+m"(m_Lock)
		);
		return ! ret;
	}
	void Release( void )
	{
		m_Lock = false;
	}
	void Reset( bool start_s = false )
	{
		m_Lock = start_s;
	}
public:
	SpinLock_t( void ) : m_Lock() {}
	~SpinLock_t( void ) {}
protected:
	volatile bool	m_Lock;
};

#else
	#error	Not define or unrecognized PCE_CFG_OS_XXX
#endif

}	// Tools
}	// PCE_Lib_CurrentCEnvironment
}	// CEnv_Def
}	// PCE_Lib

#endif	// __20100830_DEF_SPINLOCK_H__
